#!/usr/bin/python
# DART and DFP by Google traffic generator
# Quick and dirty script version
# Google for python progressbar, download and install, it's a pre-req

import os
import re
import sys
import urllib
import urllib2
import random
import linecache
from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed, RotatingMarker, ReverseBar
import time
import getopt

randInt = 15  # The maximum amount of seconds between HTTP requests (a random number will be chosen between 1 and randInt)

def formRequest(url,ip_file,ua_file,tag):

    fIP = open(ip_file,'rU')
    fUA = open(ua_file,'rU')

    ipIndex = random.randint(1,len(fIP.readlines()))  # Randomly choosing an IP address
    fIP.close()
    srcIpAddress = linecache.getline(ip_file,ipIndex).rstrip()
    
    uaIndex = random.randint(1,len(fUA.readlines()))  # Randomly choosing a user agent
    fUA.close()
    userAgent = linecache.getline(ua_file,uaIndex).rstrip()

    req = urllib2.Request(url)
    req.add_header('X-User-IP',srcIpAddress)
    req.add_header('User-Agent',userAgent)

    clickUrl = tag.replace("/ad/","/jump/")
    reqClick = urllib2.Request(clickUrl)
    reqClick.add_header('X-User-IP',srcIpAddress)
    reqClick.add_header('User-Agent',userAgent)
    
    return req,reqClick

def formMobileRequest(url,ip_file,ua_file,tag):

    getGAM = "http://pubads.g.doubleclick.net/gampad/ads?"
    markup = "&markup=html"
    urlSep = url.partition(";")
    iu = "&iu=" + urllib.quote_plus(urlSep[0])
    prev_ius = "&prev_ius=" + urllib.quote_plus(urlSep[0])
    prev_iu_szs = "&prev_iu_szs=" + urlSep[2].partition("=")[2]
    output = "&output=html"
    impl = "&impl=ifr"
    m_ast = "&m_ast=srv"
    gdfp_req = "&gdfp_req=1"
        
    fIP = open(ip_file,'rU')
    fUA = open(ua_file,'rU')

    ipIndex = random.randint(1,len(fIP.readlines()))  # Randomly choosing an IP address
    fIP.close()
    srcIpAddress = linecache.getline(ip_file,ipIndex).rstrip()
    
    uaIndex = random.randint(1,len(fUA.readlines()))  # Randomly choosing a user agent
    fUA.close()
    userAgent = linecache.getline(ua_file,uaIndex).rstrip()

    ip = "&ip=" + srcIpAddress
    useragent = "&useragent=" + urllib.quote_plus(userAgent)
    urlv = "&url=" + urllib.quote_plus("http://172.25.136.72/mobtrafgen.php")
    dt = int(round(time.time()))
    correlator = dt

    totalUrl = getGAM + markup + iu + output + impl + m_ast + gdfp_req + ip + useragent + urlv + prev_ius + prev_iu_szs + "&dt=" + str(dt) + "&correlator=" + str(correlator)

    req = urllib2.Request(totalUrl)
    
    req.add_header('X-User-IP',srcIpAddress)
    req.add_header('User-Agent',userAgent)
    req.add_header('Host','pubads.g.doubleclick.net')
    
    return req

def tagsTrafGen(tags,ip_file,ua_file,click,impressions):
    currImp = 0
    currTime = 0
          
    pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=int(impressions)).start()

    while (currImp < int(impressions)):
        for tag in tags:
            req,reqClick = formRequest(tag, ip_file, ua_file, tag)
            r = urllib2.urlopen(req)
            if click == 1:
                if random.randint(0,100) > 95:  # Randomly clicking on URLs
                    time.sleep(random.random())
                    r2 = urllib2.urlopen(reqClick)       
            
        timer = random.randint(1,randInt)
        time.sleep(timer)
        currImp = currImp + 1
        pbar.update(currImp)
       
    pbar.finish()
    print "\n"
    sys.exit(0)

def urlTrafGen(url,ip_file,ua_file,click,impressions):
    currImp = 0
    currTime = 0

    pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=int(impressions)).start()
    
    while (currImp < int(impressions)):
        req,reqClick = formRequest(url, ip_file, ua_file, url)
        r = urllib2.urlopen(req)
        if click == 1:
            if random.randint(0,100) > 95:  # Randomly clicking on URLs
                time.sleep(random.random())
                r2 = urllib2.urlopen(reqClick)       
        timer = random.randint(1,randInt)
        time.sleep(timer)
        currImp = currImp + 1
        pbar.update(currImp)

    pbar.finish()
    print "\n"
    sys.exit(0)

def mobTrafGen(tags, ip_file, ua_file, impressions):
    currImp = 0
    currTime = 0

    pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=int(impressions)).start()
    while (currImp < int(impressions)):
        for tag in tags:
            req = formMobileRequest(tag, ip_file, ua_file, tag)
            r = urllib2.urlopen(req)
           
        timer = random.randint(1,randInt)
        time.sleep(timer)
        currImp = currImp + 1
        pbar.update(currImp)
       
    pbar.finish()
    print "\n"
    sys.exit(0)

def usage():
        print ' '
        print 'Usage: ' + sys.argv[0] + ' [options] <tags-file> <ip-address-file> <user-agent-file> <impressions>'
        print ' ' 
        print '    tags-file: the name of the file that has the tags'
        print '    ip-address-file: the name of the file that has the IPs'
        print '    user-agent: the name of the file that has the UAs'
        print '    impressions: the amount of GET requests you want to generate'
        print ' '
        print '    Defaults: input from file with tags, no clicks'
        print ' '
        print 'Options: '
        print '\t -c   Generate clicks (around 5% of impressions)'
        print '\t -u   Generate pageviews to a URL, use a full URL instead of a file'
        print '\t -m   Generate impressions for mobile inventory (no clicks, c flag will be ignored)'
        print ' '
        return

def main():
    click = 0
    mobileInv = 0
    singleURL = 0
    args = sys.argv[1:]
    
    if len(sys.argv) <= 4:
        usage()
        sys.exit(2)

    try:
        opts, args = getopt.getopt(sys.argv[1:], "cu:m", ["click", "url=", "mobile"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for o,v in opts:
        if o in ("-c","--click"):
            click = 1
        if o in ("-u","--url"):
            singleURL = v
        if o in ("-m","--mobile"):
            mobileInv = 1
            
    if singleURL != 0:
         ip_file = args[0]
         ua_file = args[1]
         impressions = args[2]
         urlTrafGen(singleURL, ip_file, ua_file, click, impressions)
    else:
         tags_url = args[0]
         ip_file = args[1]
         ua_file = args[2]
         impressions = args[3]
         tags = []
         fTags = open(tags_url,'rU')
         print "\nThe following tags have been loaded: "
         i = 0
         if mobileInv == 0:
             for line in fTags:
                 tags.append(line.rstrip()+"ord=" +  str(random.randint(10000,9999999999)) + "?" )
                 print line.rstrip()
                 print " "
             fTags.close()
             tagsTrafGen(tags, ip_file, ua_file, click, impressions)

         else:
             for line in fTags:
                 tags.append(line.rstrip())
                 print line.rstrip()
             print " "
             fTags.close()
             mobTrafGen(tags, ip_file, ua_file, impressions)

if __name__ == '__main__':
  main()
